package com.study.springboot2;

import com.study.springboot2.Springboot2Application;
import com.study.springboot2.service.UserService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * RedisController Tester.
 *
 * @author <Authors name>
 * @version 1.0
 * @since <pre>九月 23, 2019</pre>
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springboot2Application.class)
//首先在类名上加入注解@RunWith和@SpringBootTest，后者需指定springboot启动类
public class RedisControllerTest {
    /**
     * 模拟mvc测试对象
     */
    private MockMvc mockMvc;

    /**
     * web项目上下文
     */
    @Autowired
    private WebApplicationContext webApplicationContext;

    /**
     * 业务数据接口
     */
    @Autowired
    private UserService userService;

    @Before
    public void before() throws Exception {
        //获取mockmvc对象实例
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @After
    public void after() throws Exception {
    }

    /**
     * Method: getRedis()
     */
    @Test
    public void testGetRedis() throws Exception {
//TODO: Test goes here... 
    }

    /**
     * Method: save()
     */
    @Test
    public void testSave() throws Exception {
        MvcResult mvcResult = mockMvc
                .perform(// 1
                        MockMvcRequestBuilders.get("/redis/saveMysql") // 2
                        .param("name","text")        // 3
                        .param("age","33")        // 3
                )
                .andReturn();// 4

        int status = mvcResult.getResponse().getStatus(); // 5
        String responseString = mvcResult.getResponse().getContentAsString(); // 6

        Assert.assertEquals("请求错误", 200, status); // 7
        Assert.assertEquals("返回结果不一致", "{\"status\":\"1\",\"data\":\"User(name=text, age=33)\",\"msg\":\"执行成功\"}", responseString); // 8
    }

    /**
     * Method: get()
     */
    @Test
    public void testGet() throws Exception {
        MvcResult mvcResult = mockMvc
                .perform(// 1
                        MockMvcRequestBuilders.get("/redis/getMysql") // 2
                        //.param("name","getList")        // 3
                )
                .andReturn();// 4

        int status = mvcResult.getResponse().getStatus(); // 5
        String responseString = mvcResult.getResponse().getContentAsString(); // 6

        Assert.assertEquals("请求错误", 200, status); // 7

    }


    @Test
    public void testView() throws Exception {
        MvcResult mvcResult = mockMvc
                .perform(// 1
                        MockMvcRequestBuilders.get("/redis/view") // 2
                        //.param("name","getList")        // 3
                )
                .andReturn();// 4

        int status = mvcResult.getResponse().getStatus(); // 5
        String responseString = mvcResult.getResponse().getContentAsString(); // 6

        Assert.assertEquals("请求错误", 200, status); // 7
        //Assert.assertEquals("返回结果不一致", "/firstPage", responseString); // 8
    }

} 
